package com.sxsy.datasource.util;

import com.sxsy.datasource.bean.SwjcBean;
import com.sxsy.datasource.bean.WsjcBean;
import com.sxsy.datasource.biz.WsjcData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@Repository
public class WsjcFileUtil {
    ArrayList<String> fileNameList = new ArrayList();
    static String BJFileName = null;
    static String SSFileName = null;
    public static Map<String, String[]> Devmap = new HashMap<String, String[]>();
    @Autowired
    private WsjcData wsjcData;
    public int Keyindex = 1;
    public int Wzindex = 2;
   // public int Dwindex = 8;
    public int Valueindex = 6;
    public int Dateindex = 5;
    int BjCodeindex = 1;
    int BjWzindex = 2;
    int BjValueindex = 4;
    int BjStimeindex = 5;
    int BjEtimeindex = 6;
    public   void DEVfileAnalysis() throws IOException {
        String[] oldstr = null;
        String[] newstr = null;
        getAllFileName("D:\\shareData\\WSJC\\", fileNameList);
        Map<String, String[]> Devfilemap = new HashMap<String, String[]>();
        if (fileNameList.isEmpty()) {
            return;
        }
        for (int j = 0; j <= fileNameList.size(); j++) {
            File file = null;
            BufferedReader br = null;//构造一个BufferedReader类来读取文件
            try {
                if (fileNameList.get(j) == null) {
                                return;
                            }

                file = new File("D:\\shareData\\WSJC\\" + fileNameList.get(j));
                br = new BufferedReader(new FileReader("D:\\shareData\\WSJC\\" + fileNameList.get(j)));
                String s = null;
                int i = 0;
                while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
                    if (i > 0) {
                        String[] str = s.split("\\;");
                        if (str[2] != null) {
                            Devfilemap.put(str[Keyindex], str);
                        }
                    }
                    i++;
                }

                //正向比较
                List<WsjcBean> wsjcdata = wsjcData.DoesKeyExit();
                if (wsjcdata.isEmpty()) {
                    for (Entry<String, String[]> Newentry : Devfilemap.entrySet()) {
                        Devmap.put(Newentry.getKey(), Devfilemap.get(Newentry.getKey()));
                        //数据库增加方法（key，WZ，DW）
                        String addkey = Newentry.getKey();
                        String wz = Devmap.get(Newentry.getKey())[Wzindex];
                        //List<String> KeyList = new ArrayList<>();
                        wsjcData.RtsaveNewDevkeyValue(addkey, wz);
                    }

                }else {
                    if (Devmap.size() == 0) {
                        for (int a = 0 ; a<wsjcdata.size(); a++){

                            String[] strlist = new String[wsjcdata.size()];

                            Devmap.put(wsjcdata.get(a).getCjgjk_Wsjc_Rt_Bm(),strlist);
                        }

                    }
                    for(Entry<String, String[]> Newentry : Devfilemap.entrySet()) {
                        if(Devmap.containsKey(Newentry.getKey())!=true) {
                            Devmap.put(Newentry.getKey(), Devfilemap.get(Newentry.getKey()));
                            //数据库增加方法（key，WZ，DW）
                            String addkey = Newentry.getKey();
                            String wz = Devmap.get(Newentry.getKey())[Wzindex];
                            //List<String> KeyList = new ArrayList<>();
                            wsjcData.RtsaveNewDevkeyValue(addkey, wz);
                        }
                    }
                    //反向比较
                    for(Entry<String, String[]> Oldentry : Devmap.entrySet()) {
                        //System.out.println("Devmap:"+Oldentry.getKey());
                        if(Devfilemap.containsKey(Oldentry.getKey())!=true) {
                            String rmkey = Oldentry.getKey();
                            //数据库减方法（where key  = 。。。）
                           // System.out.println(rmkey);
                            wsjcData.RtdeleteRow(rmkey);
                        }

                    }

                }
                Devmap = Devfilemap;
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                fileNameList.clear();
                SSFileName = null;
                br.close();
                file.delete();
            }


        }
        SSfileAnalysis();
    }


    public   void BJfileAnalysis() throws IOException {
        // //code wz value stime etime
        File file = null;
        BufferedReader br = null;//构造一个BufferedReader类来读取文件
        try {
            getAllFileName("D:\\shareData\\WSJC\\",fileNameList);
            if (BJFileName == null){
                return;
            }
            file = new File("D:\\shareData\\WSJC\\"+BJFileName);
            br = new BufferedReader(new FileReader("D:\\shareData\\WSJC\\"+BJFileName));
            String s = null;
            int i = 0;
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                if(i>0){
                    String[] str = s.split("\\;");
                    if(str[1]!=null){
                        wsjcData.insertBjdata(str[BjCodeindex],str[BjWzindex],str[BjValueindex],str[BjStimeindex],str[BjEtimeindex]);
                    }
                }

                i++;

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            br.close();
            BJFileName = null;
            file.delete();
        }


    }
    public   void SSfileAnalysis() throws IOException {
        File file = null;
        BufferedReader br = null;//构造一个BufferedReader类来读取文件
        try {
            getAllFileName("D:\\shareData\\WSJC\\",fileNameList);
            if (SSFileName == null){
                return;
            }
            file = new File("D:\\shareData\\WSJC\\"+SSFileName);
            br = new BufferedReader(new FileReader("D:\\shareData\\WSJC\\"+SSFileName));
            String s = null;
            int i = 0;
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                if(i>0){
                    String[] str = s.split("\\;");
                    if(str[1]!=null){
                        if(Devmap.containsKey(str[1])){
                            String[] tmp = Devmap.get(str[1]);
                            wsjcData.HistoryTableinsert(tmp[Keyindex],tmp[Wzindex],str[Valueindex],str[Dateindex]);
                            wsjcData.RtsaveNewSsKeyValue(str[Keyindex],tmp[Wzindex],str[Valueindex],str[Dateindex]);
                        }


                    }
                }

                i++;

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            br.close();
            file.delete();
        }


    }
    public static void getAllFileName(String path,ArrayList<String> fileNameList) {
        //ArrayList<String> files = new ArrayList<String>();
       // boolean flag = false;
        File file = new File(path);
        File[] tempList = file.listFiles();

        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
//              System.out.println("文     件：" + tempList[i]);
                //fileNameList.add(tempList[i].toString());
                if(tempList[i].getName().contains("AQBJ")){
                    BJFileName = tempList[i].getName();
                }
                if(tempList[i].getName().contains("AQSS")){
                    SSFileName = tempList[i].getName();
                }
                if(tempList[i].getName().contains("AQMT")){
                    fileNameList.add(tempList[i].getName());
                }

            }
            if (tempList[i].isDirectory()) {
                getAllFileName(tempList[i].getAbsolutePath(),fileNameList);
            }
        }
        return;
    }
}
